package com.example.demo.controller;

import com.example.demo.Res.Res;
import com.example.demo.dto.RoleDto;
import com.example.demo.entity.Role;
import com.example.demo.service.RoleService;
import com.example.demo.vo.RoleMenuParams;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/role/")
public class RoleController {
    private RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @CrossOrigin
    @GetMapping("select")
    Res select() {
        final List<RoleDto> list = roleService.selectRole();
        if(list.size() > 0) {
            return Res.litOk("", list);
        } else {
            return Res.bigOn("获取角色列表失败", null);
        }
    }

    @CrossOrigin
    @PostMapping("delete")
    Res delete(@RequestParam String id) {
        final int i = roleService.deleteRoleById(id);
        if(i > 0) {
            return Res.litOk("删除角色成功", null);
        } else {
            return Res.bigOn("删除角色列表失败", null);
        }
    }
    @CrossOrigin
    @PostMapping("update")
    Res updates(@RequestBody RoleDto roleDto) throws ParseException {
        final int i = roleService.insertRole(roleDto);
        if(i > 0) {
            return Res.litOk("删除角色成功", null);
        } else {
            return Res.bigOn("删除角色列表失败", null);
        }
    }

    @CrossOrigin
    @GetMapping("like")
    Res likes(@RequestParam String key) {
        final List<RoleDto> list = roleService.likeRole(key);

        return Res.litOk("", list);
    }

    @CrossOrigin
    @PostMapping("updateRole")
    Res roleUpdate(@RequestBody RoleMenuParams roleMenuParams) {
        System.out.println(222);
        System.out.println(roleMenuParams);

        final int i = roleService.updateRoleMenu(roleMenuParams);
        if(i > 0) {
            return Res.litOk("更新菜单成功", null);
        } else {
            return Res.bigOn("更新菜单失败", null);
        }
    }

    @CrossOrigin
    @PostMapping("updateRoleInfo")
    Res roleUpdate(@RequestBody Role roleMenuParams) {
        final int i = roleService.updateRoleInfo(roleMenuParams);
        if(i > 0) {
            return Res.litOk("更新角色信息成功", null);
        } else {
            return Res.bigOn("更新角色信息失败", null);
        }
    }

    @CrossOrigin
    @PostMapping("add")
    Res add(@RequestBody Role roleMenuParams) {
        final int i = roleService.addRole(roleMenuParams);
        if(i > 0) {
            return Res.litOk("添加角色成功", null);
        } else {
            return Res.bigOn("更新角色失败", null);
        }
    }
}
